﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SqlScriptVault.Services
{
    public class EventService
    {
        public static string NewEvent = "NewEvent";
        public static string LoadEvent = "LoadEvent";
        public static string SaveEvent = "SaveEvent";
        public static string ExecuteEvent = "ExecuteEvent";
        public static string OpenHistoryEvent = "OpenHistoryEvent";
        public static string ScriptVaultChangedEvent = "ScriptVaultChangedEvent";
        public static string TitleChangedEvent = "TitleChangedEvent";
        public static string PasswordEvent = "PasswordEvent";

        private static EventService _instance = new EventService();

        public static EventService Instance
        {
            get
            {
                return _instance;
            }
        }

        private Dictionary<string, Func<object, object>> _subscriptions = new Dictionary<string, Func<object, object>>();
        
        private EventService()
        {
        }

        public void Subscribe(string eventName, Func<object, object> func)
        {
            if (!_subscriptions.ContainsKey(eventName))
            {
                _subscriptions.Add(eventName, func);
            }
        }

        public void Unsubscribe(string eventName)
        {
            if (_subscriptions.ContainsKey(eventName))
            {
                _subscriptions.Remove(eventName);
            }
        }

        public object Publish(string eventName, object param)
        {
            if (_subscriptions.ContainsKey(eventName))
            {
                return _subscriptions[eventName](param);
            }

            return null;
        }
    }
}
